﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsFormsApp1.model;

namespace WindowsFormsApp1.helper
{
    class DataHelper
    {
        public List<ShiGongTianChuangModel> JobList;
        public List<JobListHeaderModel> JobListHeader;
        public List<JobListHeaderModel> JobTechnologicalProcesslListHeader;

        private string jobListFileName = ".\\JobModelList.xml";

        private string jobListHeaderFileName = ".\\JobModelListHeader.xml";

        private string JobTechnologicalProcessHeaderFileName = ".\\JobTechnologicalProcesslListHeader.xml";

        // 定义一个静态变量来保存类的实例
        private static DataHelper uniqueInstance;

        // 定义一个标识确保线程同步
        private static readonly object locker = new object();

        // 定义私有构造函数，使外界不能创建该类实例
        private DataHelper()
        {
            //作业列表内容
            if (File.Exists(jobListFileName))
            {
                JobList = XmlHelper.XMLFlieToObject<List<ShiGongTianChuangModel>>(jobListFileName, Encoding.Unicode);
            }else
            {
                FileStream fs = new FileStream(jobListFileName, FileMode.CreateNew);
                JobList = new List<ShiGongTianChuangModel>();
                fs.Close();
            }

            //作业列表表头
            if (File.Exists(jobListHeaderFileName))
            {
                JobListHeader = XmlHelper.XMLFlieToObject<List<JobListHeaderModel>>(jobListHeaderFileName, Encoding.Unicode);
            }
            else
            {
                
                JobListHeader = new List<JobListHeaderModel>();
                JobListHeaderModel j = new JobListHeaderModel();
                j.HeaderName = "施工名称";
                j.Width = 60;
                j.TextAlign = 0;
                JobListHeaderModel j1 = new JobListHeaderModel();
                j1.HeaderName = "施工编号";
                j1.Width = 60;
                j1.TextAlign = 0;

                JobListHeaderModel j2 = new JobListHeaderModel();
                j2.HeaderName = "施工地点";
                j2.Width = 60;
                j2.TextAlign = 0;

                JobListHeaderModel j3 = new JobListHeaderModel();
                j3.HeaderName = "作业性质";
                j3.Width = 60;
                j3.TextAlign = 0;
                JobListHeaderModel j7 = new JobListHeaderModel();
                j7.HeaderName = "施工单位主体";
                j7.Width = 60;
                j7.TextAlign = 0;
                JobListHeaderModel j4 = new JobListHeaderModel();
                j4.HeaderName = "施工单位配合";
                j4.Width = 60;
                j4.TextAlign = 0;
                JobListHeaderModel j5 = new JobListHeaderModel();
                j5.HeaderName = "开始时间";
                j5.Width = 60;
                j5.TextAlign = 0;
                JobListHeaderModel j6 = new JobListHeaderModel();
                j6.HeaderName = "总时长";
                j6.Width = 60;
                j6.TextAlign = 0;

                JobListHeader.Add(j);
                JobListHeader.Add(j1);
                JobListHeader.Add(j2);
                JobListHeader.Add(j3);
                JobListHeader.Add(j7);
                JobListHeader.Add(j4);
                JobListHeader.Add(j5);
                JobListHeader.Add(j6);


                XmlHelper.ObjectToXMLFile(JobListHeader, jobListHeaderFileName, Encoding.Unicode);
              
            }

            //作业流程列表表头
            if (File.Exists(JobTechnologicalProcessHeaderFileName))
            {
                JobTechnologicalProcesslListHeader = XmlHelper.XMLFlieToObject<List<JobListHeaderModel>>(JobTechnologicalProcessHeaderFileName, Encoding.Unicode);
            }
            else
            {
                JobTechnologicalProcesslListHeader = new List<JobListHeaderModel>();
                JobListHeaderModel j = new JobListHeaderModel();
                j.HeaderName = "施工名称";
                j.Width = 60;
                j.TextAlign = 0;
                

                JobListHeaderModel j1 = new JobListHeaderModel();
                j1.HeaderName = "施工单位";
                j1.Width = 60;
                j1.TextAlign = 0;

                JobListHeaderModel j2 = new JobListHeaderModel();
                j2.HeaderName = "施工地点";
                j2.Width = 60;
                j2.TextAlign = 0;

                JobTechnologicalProcesslListHeader.Add(j);
                JobTechnologicalProcesslListHeader.Add(j1);
                JobTechnologicalProcesslListHeader.Add(j2);

                for (int i = 1; i <= 5; i++)
                {
                    JobListHeaderModel j3 = new JobListHeaderModel();
                    j3.HeaderName = "流程" + i.ToString();
                    j3.Width = 60;
                    j3.TextAlign = 0;
                    JobTechnologicalProcesslListHeader.Add(j3);
                }
                
                XmlHelper.ObjectToXMLFile(JobTechnologicalProcesslListHeader, JobTechnologicalProcessHeaderFileName, Encoding.Unicode);

            }

        }

        /// <summary>
        /// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点
        /// </summary>
        /// <returns></returns>
        public static DataHelper GetInstance()
        {
            // 当第一个线程运行到这里时，此时会对locker对象 "加锁"，
            // 当第二个线程运行该方法时，首先检测到locker对象为"加锁"状态，该线程就会挂起等待第一个线程解锁
            // lock语句运行完之后（即线程运行完之后）会对该对象"解锁"
            lock (locker)
            {
                // 如果类的实例不存在则创建，否则直接返回
                if (uniqueInstance == null)
                {
                    uniqueInstance = new DataHelper();
                }
            }

            return uniqueInstance;
        }

        public bool refrashJobList()
        {
            return XmlHelper.ObjectToXMLFile(JobList, jobListFileName, Encoding.Unicode);
        }
    }
}
